package game;
import java.lang.Double;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;

/**
 * 
 * @author Shun Fan
 *	parses level from text files
 */
public class TxtParser{
	private File myFile;
	private double path_speed;
	private int path_x_offset;
    private int path_y_offset;
	private int fishToSaveAtStartOfLevel;
	private int path_start_t;
	private int num_path_points;
	private PathPoint[] path;
	private String pathPointsString;
	private ArrayList<Point2D.Double> pathPoints = new ArrayList<Point2D.Double>();
	
	public TxtParser() {
		
	}
	
	public TxtParser(File sourceFile) {
		pathPoints = new ArrayList<Point2D.Double>();
		myFile = (File) sourceFile;
		try {			
			Scanner textScanner = new Scanner(myFile).useDelimiter("(\\s*=\\s*)|(;\\s*)");
			
			parseVariables(textScanner);		
			createPathPointsArray(pathPointsString);
			
		} catch (FileNotFoundException e) {
			System.out.println("text parser could not find file");
			//e.printStackTrace();
		}
	}
	

	private void parseVariables(Scanner textScanner) {
		textScanner.next();
		fishToSaveAtStartOfLevel = Integer.parseInt(textScanner.next());
		textScanner.next();
		path_speed = Double.parseDouble(textScanner.next());
		textScanner.next();
		path_x_offset = Integer.parseInt(textScanner.next());
		textScanner.next();
		path_y_offset = Integer.parseInt(textScanner.next());
		textScanner.next();
		path_start_t = Integer.parseInt(textScanner.next());
		textScanner.next();
		num_path_points = Integer.parseInt(textScanner.next());
		textScanner.next();
		pathPointsString = textScanner.next();
		
	}

	private void createPathPointsArray(String pathPointsString) {
    	Scanner pointsScanner = new Scanner(pathPointsString).useDelimiter("(\\s*,\\s*)|(\\s+)");
		
    	while(pointsScanner.hasNext()) {
    		double parsedXvalue = new Double(pointsScanner.next());
			double parsedYvalue = new Double(pointsScanner.next());
			Point2D.Double newPoint = new Point2D.Double(parsedXvalue, parsedYvalue);
    		//System.out.println("pointx: " + parsedXvalue + ", pointy: " + parsedYvalue);
    		pathPoints.add(newPoint);
    	}
    	//System.out.println("pathpoints size: " + pathPoints.size());
	}
	
	public double getBubblePathSpeed() {
    	return path_speed;
    }
    
    public int getFishLeftToSave() {
    	return fishToSaveAtStartOfLevel;
    }
    
    public int getBubblePathStartPos() {
    	return path_start_t;
    }
    
    public int getNumberOfBubblePathPoints() {
    	return num_path_points;
    }
    
    public ArrayList<Point2D.Double> getBubblePathPoints(){
    	return pathPoints;
    }

	public int getPathXOffset() {
		return path_x_offset;
	}
	
	public int getPathYOffset() {
		return path_y_offset;
	}
}
